package edu.uoc.tdp.pac4.client.stats;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import javax.swing.text.DateFormatter;

import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;

public class StatsStudent extends JPanel {
	private JFormattedTextField dataIniciCurs;
	
	private JPanel filterPanel = null;
	private JPanel resultsPanel = null;
	private JScrollPane resultsScroll = null;

	private List<StatResultData> statsStudentResults = null;
	SimpleDateFormat dataFormatter = new SimpleDateFormat("dd/MM/yyyy");

	private JButton btnNewButton = null;
	private JRadioButton allCoursesBtn = null;
	private JRadioButton finishedCoursesBtn = null;
	private JRadioButton notFinishedBtn = null;
			

	// Connection to the services
	ClientConnectionHelper connectionHelper_ = null;

	public StatsStudent(ClientConnectionHelper connectionHelper) {
		setLayout(new BorderLayout(0, 0));

		connectionHelper_ = connectionHelper;

		// Filters
		filterPanel = new JPanel();
		filterPanel.setBorder(new TitledBorder(null, LanguageUtils.getMessage("client.statistics.filters"), TitledBorder.LEADING,TitledBorder.TOP, null, null));
		add(filterPanel, BorderLayout.NORTH);
		filterPanel.setLayout(new GridLayout(2, 4, 0, 0));

		// Primera Fila Filters
		JLabel lblNewLabel = new JLabel(LanguageUtils.getMessage("client.statistics.datainici"));
		filterPanel.add(lblNewLabel);

		DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
	    DateFormatter df = new DateFormatter(format);
	    dataIniciCurs = new JFormattedTextField(df);
	        
		//dataIniciCurs = new JTextField();
		filterPanel.add(dataIniciCurs);
		filterPanel.add(new JLabel(" (dd/MM/yyyy)"));
		filterPanel.add(new JLabel());

		// Segona Fila Filters
		JLabel label = new JLabel(LanguageUtils.getMessage("client.statistics.curs.tipuscurs"));
		filterPanel.add(label);

		ButtonGroup buttonGroup = new ButtonGroup();
		allCoursesBtn = new JRadioButton(LanguageUtils.getMessage("client.statistics.curs.tots"));
		buttonGroup.add(allCoursesBtn);
		filterPanel.add(allCoursesBtn);

		finishedCoursesBtn = new JRadioButton(LanguageUtils.getMessage("client.statistics.curs.acabats"));
		buttonGroup.add(finishedCoursesBtn);
		filterPanel.add(finishedCoursesBtn);

		notFinishedBtn = new JRadioButton(LanguageUtils.getMessage("client.statistics.curs.pendentsfinal"));
		buttonGroup.add(notFinishedBtn);
		filterPanel.add(notFinishedBtn);
		allCoursesBtn.setSelected(true);

		// Results
		resultsPanel = new JPanel();
		resultsPanel.setLayout(new BorderLayout());

		resultsScroll = new JScrollPane(getResultsTable());
		resultsScroll.setViewportView(getResultsTable());
		resultsScroll.setColumnHeaderView(getResultsTable().getTableHeader());

		resultsPanel.add(resultsScroll);

		add(resultsPanel);

		JToolBar toolBar = new JToolBar();
		add(toolBar, BorderLayout.SOUTH);

		btnNewButton = new JButton(LanguageUtils.getMessage("client.statistics.cerca"));
		btnNewButton.setHorizontalAlignment(SwingConstants.RIGHT);
		toolBar.add(btnNewButton);
		JLabel info = new JLabel("  " + LanguageUtils.getMessage("client.stat.alumne.1"));
		toolBar.add(info);
		
		btnNewButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == btnNewButton) {
					try {
						Date date = null;
						if(dataIniciCurs != null && dataIniciCurs.getText() !=null && !"".equals(dataIniciCurs.getText())){
							 date = dataFormatter.parse(dataIniciCurs.getText());
						}
						Integer courseType = null;
						//Codes: All=3, Finished=2, Not finished=1
						if(allCoursesBtn.isSelected()){
							courseType = new Integer(3);
						}else if(finishedCoursesBtn.isSelected()){
							courseType = new Integer(2);
						}else if(notFinishedBtn.isSelected()){
							courseType = new Integer(1);
						}
						System.out.println("Sending Params: ");
						System.out.println("Date: " + date);
						System.out.println("C. Type: " + courseType);
						String dniAlumne = ClientConnectionHelper.getLoggedUser().getNIF();
						statsStudentResults = connectionHelper_.getStatsManager().getStudentResult(date, courseType, dniAlumne);
						
						resultsScroll.setViewportView(getResultsTable());
					} catch (RemoteException e1) {
						// Remote Exception: Server error.
						JOptionPane.showMessageDialog(null, LanguageUtils
								.getMessage("client.error.message.remote"),
								LanguageUtils.getMessage("client.error.title"),
								JOptionPane.ERROR_MESSAGE);
					} catch (Exception e2) {
						JOptionPane.showMessageDialog(null, LanguageUtils
								.getMessage("client.error.message"),
								LanguageUtils.getMessage("client.error.title"),
								JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
	}

	private JTable getResultsTable() {
		JTable tableStudentResults = null;
		if (tableStudentResults == null) {
			tableStudentResults = new JTable();
			StatStudentModel model = new StatStudentModel(statsStudentResults);
			tableStudentResults.setModel(model);

		}
		tableStudentResults.setEnabled(false);
		return tableStudentResults;
	}
}
